//
//  Cliente.m
//  InterventixApp
//
//  Created by Diego Falcinelli on 08/05/13.
//
//

#import "Cliente.h"
#import "Syncronize.h"
#import "Utente.h"

@implementation Cliente

-(NSMutableDictionary *) convertToDictionary{
    NSMutableDictionary *diz = [NSMutableDictionary dictionaryWithCapacity:0];
    [diz setObject:self.idCliente       forKey:@"idcliente"];
    [diz setObject:self.nominativo      forKey:@"nominativo"];
    [diz setObject:self.codiceFiscale   forKey:@"codicefiscale"];
    [diz setObject:self.partitaIva      forKey:@"partitaiva"];
    [diz setObject:self.telefono        forKey:@"telefono"];
    [diz setObject:self.fax             forKey:@"fax"];
    [diz setObject:self.email           forKey:@"email"];
    [diz setObject:self.referente       forKey:@"referente"];
    [diz setObject:self.citta           forKey:@"citta"];
    [diz setObject:self.indirizzo       forKey:@"indirizzo"];
    [diz setObject:self.ufficio         forKey:@"ufficio"];
    [diz setObject:self.note            forKey:@"note"];
    [diz setObject:self.revisione       forKey:@"revisione"];
    
    if(self.cancellato){
        [diz setObject:@true            forKey:@"cancellato"];
    }else{
        [diz setObject:@false           forKey:@"cancellato"];
    }
    
    if(self.cestinato){
        [diz setObject:@true            forKey:@"cestinato"];
    }else{
        [diz setObject:@false           forKey:@"cestinato"];
    }
    return diz;
}

-(void) readFromMutableDictionary:(NSMutableDictionary *) diz{
    self.idCliente      = [NSNumber numberWithInt:[[diz objectForKey:@"idcliente"] intValue]];
    self.nominativo     = [diz objectForKey:@"nominativo"];
    self.codiceFiscale  = [diz objectForKey:@"codicefiscale"];
    self.partitaIva     = [diz objectForKey:@"partitaiva"];
    self.fax            = [diz objectForKey:@"fax"];
    self.email          = [diz objectForKey:@"email"];
    self.referente      = [diz objectForKey:@"referente"];
    self.citta          = [diz objectForKey:@"citta"];
    self.indirizzo      = [diz objectForKey:@"indirizzo"];
    self.interno        = [diz objectForKey:@"interno"];
    self.ufficio        = [diz objectForKey:@"ufficio"];
    self.note           = [diz objectForKey:@"note"];
    self.revisione      = [NSNumber numberWithInt:[[diz objectForKey:@"revisione"] intValue]];
    self.cancellato     = [[diz objectForKey:@"cancellato"] boolValue];
    self.cestinato      = [[diz objectForKey:@"cestinato"] boolValue];
}

-(void) readFromDictionary:(NSDictionary *) diz{
    self.idCliente      = [NSNumber numberWithInt:[[diz objectForKey:@"idcliente"] intValue]];
    self.nominativo     = [diz objectForKey:@"nominativo"];
    self.codiceFiscale  = [diz objectForKey:@"codicefiscale"];
    self.partitaIva     = [diz objectForKey:@"partitaiva"];
    self.fax            = [diz objectForKey:@"fax"];
    self.email          = [diz objectForKey:@"email"];
    self.referente      = [diz objectForKey:@"referente"];
    self.citta          = [diz objectForKey:@"citta"];
    self.indirizzo      = [diz objectForKey:@"indirizzo"];
    self.interno        = [diz objectForKey:@"interno"];
    self.ufficio        = [diz objectForKey:@"ufficio"];
    self.note           = [diz objectForKey:@"note"];
    self.revisione      = [NSNumber numberWithInt:[[diz objectForKey:@"revisione"] intValue]];
    self.cancellato     = [[diz objectForKey:@"cancellato"] boolValue];
    self.cestinato      = [[diz objectForKey:@"cestinato"] boolValue];
}

-(void) load{
    //Utente *ulog =[Syncronize getUserLog];
    //NSArray *all = [Syncronize getClients:ulog];
    /*for(Cliente *us in all){
        if([us.idCliente longLongValue] == [self.idCliente longLongValue]){
            self.nominativo = us.nominativo;
            self.codiceFiscale = us.codiceFiscale;
            self.partitaIva = us.partitaIva;
            self.fax = us.fax;
            self.email = us.email;
            self.referente = us.referente;
            self.citta = us.citta;
            self.indirizzo = us.indirizzo;
            self.interno = us.interno;
            self.ufficio = us.ufficio;
            self.note = us.note;
            self.revisione = us.revisione;
            self.cancellato = us.cancellato;
            self.cestinato = us.cestinato;
        }
    }*/
}

-(unsigned long long) hashcode{
    unsigned long long h = [self.idCliente hash];
    h+= [self.nominativo hash];
    h+= [self.codiceFiscale hash];
    h+= [self.partitaIva hash];
    h+= [self.fax hash];
    h+= [self.email hash];
    h+= [self.referente hash];
    h+= [self.citta hash];
    h+= [self.revisione hash] * 231;
    h+= [self.indirizzo hash];
    h+= [self.interno hash];
    h+= [self.ufficio hash];
    h+= [self.note hash];
    h+= self.cancellato + self.cestinato;
    return h;
}

@end
